extends Node



func shake(thing: Node2D, strength: float, duration: float = 0.2) -> void:
	if not thing:
		return

	var orig_pos := thing.position
	var tween := thing.create_tween()
	var shake_count := 10
	for i in shake_count:
		var shake_offset := Vector2(randf_range(-1.0, 1.0), randf_range(-1.0, 1.0))
		var target := orig_pos + strength * shake_offset
		if i % 2 == 0: 
			target = orig_pos
		tween.tween_property(thing, "position", target, duration / float(shake_count))
		strength *= 0.75
	
	tween.finished.connect(
		func(): 
			if thing != null:
				thing.position = orig_pos
	)
	await tween.finished


func state_play(thing: Node2D, state_type, v, d = 0) -> void:
	if not thing:
		return
	var state = Globals.ALL_STATE_TRES_DICT.state_dict[state_type].create_instance(v,d)
	var s = state.init_scene()
	thing.add_child(s)
	s.global_position = thing.global_position + Vector2(-20,-180)
	s.pivot_offset = Vector2(20,20)
	s.scale = Vector2(4,4)
	var tween := create_tween()
	tween.parallel().tween_property(s, "modulate", Color(1, 1, 1, 0), 0.1).set_delay(0.3)
	tween.parallel().tween_property(s, "scale", Vector2(1,1), 0.1).set_delay(0.3)
	
	tween.finished.connect(
		func(): 
			s.queue_free()
	)

var attack_number = preload("res://src/battle/attack_number.tscn")
func show_number(thing: Node2D, number, color: Color = Color.RED) -> void:
	if thing == null:
		return
	var p = thing.placeholder
	var n = attack_number.instantiate()
	var tween := create_tween()
	n.set_number(number)
	n.set_color(color)
	tween.tween_callback(
		func():
			p.add_child(n)
	)
	

